$(function(){
	showCondition(true, 1, 1, 1, [0, 0, 1, 1, 1]);//加载查询条件
	var param=drgUI.setAjaxData(tabId);
	compareType = $("#searchcq").children().eq(3).find("a.active").attr("data-value");//页面重新获取compareType
	$.ajax({
		url : contextPath +"/MDCGroup/findAllMDCGroup.dhc",
		type : "post",
		dataType : "json",
		async : false,
		data :{'dateFrom' : paramTabPage[tabId].dateFrom,
			 'dateTo' : paramTabPage[tabId].dateTo},
	    success : function(result){
				var data = result.data;
				var optStr = "";
				for(var i = 0;i<data.length;i++){
					var mdcCode = data[i].ccode;
					if(i==0){
						optStr = optStr + '<option value="'+data[i][0]+'" selected="selected" >'+data[i][0]+"-"+data[i][1]+'</option>';
					}else{
						optStr = optStr + '<option value="'+data[i][0]+'">'+data[i][0]+"-"+data[i][1]+'</option>';
					}
				}
				//多选下拉框初始化
				$("#"+tabId+" #mdcID").append(optStr);
				
			}
	});
	//查询科室
	$.ajax({
		url : contextPath + "/DrgsIndexCal/findAllDepByHospCode.dhc",
		type : "post",
		data:param,
		dataType : "json",
		async : false,
		success : function(result) {
			var data = result.data;
			var optStr = "";
			for(var i = 0;i<data.length;i++){
				var depCode = data[i].ccode;
				if(i==0){
					optStr = optStr + '<option value="'+data[i].DEPCODE+'" selected="selected" >'+data[i].DEPNAME+'</option>';
				}else{
					optStr = optStr + '<option value="'+data[i].DEPCODE+'" selected="selected">'+data[i].DEPNAME+'</option>';
				}
			}
			//多选下拉框初始化
			$("#"+tabId+" #depID").append(optStr);
		}
	});
	search($("#"+tabId+" #mdcID").val(),deps);//页面刷新默认加载第一个MDC
	var deps='';
	var mdcs='';
	$("#"+tabId+" #sureTargetButton").click(function(){
		if(!$("#"+tabId+" #depID").val() || !$("#"+tabId+" #mdcID").val()){
               drgUI.error('查询失败，请选择科室!');
            
		}else{
			deps=$("#"+tabId+" #depID").val().join(",");
			mdcs=$("#"+tabId+" #mdcID").val().join(",");
			
			search(mdcs,deps);
		}
	})
	$("#" + tabId + " .selectpicker").selectpicker({
		width : 220,// select框宽度
		actionsBox : true,// 全选和取消全选
		liveSearch : true,// 搜索框
		selectAllText : '全选',// selected="selected",
		deselectAllText : '取消全选',
		noneSelectedText : '请选择',
		countSelectedText : "已选中{0}个",// 选中的大于2个时，提示
		selectedTextFormat : "count > 2"

	});
	function search(mdcCode,depCode){
		var xDate = new Date();
		var cID = xDate.getTime();
		var param=jQuery.extend(true, {}, paramTabPage[tabId]);
		param["deps"]=depCode;
		param["monitorType"]="drgs";
		param["quotaCode"]="AvgDaysAvgFeeDrugRatio";
		param["compareType"]=compareType;
		param["isGetCc"]="1";
		param["mdcs"]=mdcCode;
		$.post(contextPath+"/ywcHome/getDoubleQuotaAnalysis.dhc", param).done(function(result) {
			var data = result.data ;
			var compareName = '';
			if (compareType == "year") {
				compareName = "同期";
			} else if (compareType == "month") {
				compareName = "上期";
			} else if (compareType == "manage") {
				compareName = "绩效";
			} else if (compareType == "std") {
				compareName = "标杆";
			}else if (compareType == "hosp") {
				compareName = "所选科室覆盖病组均值的";
			}
			for(var i=0;i<data.length;i++){
				data[i].ddesc = data[i].ccode+'-'+data[i].ddesc;
			}
			var chartParams={isDataZoom:true,cursor:"default"};
			var xyAxis = {"xAxis": {"code": "AvgDays", "desc": "平均住院日"}, "yAxis": {"code": "AvgFee", "desc": "次均费用（元）"}}
			quotaScatter($('#'+tabId+' #c'), xyAxis, data, "平均住院日和次均费用与"+compareName+"差值象限分析图","",chartParams);
			drawTable ($('#'+tabId+' div#t'),xyAxis,data);
			
			var xyAxis = {"xAxis": {"code": "DrugRatio", "desc": "药占比(%)"}, "yAxis": {"code": "AvgFee", "desc": "次均费用（元）"}}
			quotaScatter($('#'+tabId+' #c1'), xyAxis, data, "药占比和次均费用与"+compareName+"差值象限分析图","",chartParams);
			drawTable1 ($('#'+tabId+' div#t1'),xyAxis,data);
		});
			
    function drawTable (container,xyAx,data) {
				var drgCode1 = "";
				var drgCode2 = "";
				var drgCode3 = "";
				var drgCode4 = "";
				for (var i = 0; i < data.length; i++) {  
					var value1 = data[i][xyAx.xAxis.code];
					var value2 = data[i][xyAx.yAxis.code];
					if(value1!=null && value1!=undefined && value2!=null && value2!=undefined ){
						var v1 = parseFloat(value1)
						var v2 = parseFloat(value2);
						var drgCode = data[i].ddesc;
						if(v1>0 && v2>0){
							drgCode1 += drgCode+"、";
						}else if(v1<0 && v2>0){
							drgCode2 += drgCode+"、";
						}else if(v1<=0 && v2<=0){
							drgCode3 += drgCode+"、";
						}else if(v1>0 && v2<0){
							drgCode4 += drgCode+"、";
						}
					}
				}
			
			container.empty();
			var element = [];
			element.push("<table class='quadrant-table'>");
			element.push("<tr><td><font color = '#f05a72'><i class='icon-circle'></i></font>&nbsp;I象限:平均住院日和次均费用超标</td><td id='td1'></td></tr>");
			element.push("<tr><td><font color = '#5bc0de'><i class='icon-circle'></i></font>&nbsp;II象限:次均费用超标</td><td id='td2'></td></tr>");
			element.push("<tr><td><font color = '#66cc66'><i class='icon-circle'></i></font>&nbsp;III象限:平均住院日和次均费用未超标</td><td id='td3'></td></tr>");
			element.push("<tr><td><font color = '#fed503'><i class='icon-circle'></i></font>&nbsp;IV象限:平均住院日超标</td><td id='td4'></div></td></tr>");
			element.push("</table>");
			container.append(element.join("\n"));
			container.find('#td1').text(drgCode1.substr(0,drgCode1.length-1));
			container.find('#td2').text(drgCode2.substr(0,drgCode2.length-1));
			container.find('#td3').text(drgCode3.substr(0,drgCode3.length-1));
			container.find('#td4').text(drgCode4.substr(0,drgCode4.length-1));
		}
    
    function drawTable1 (container,xyAx,data) {
		var drgCode1 = "";
		var drgCode2 = "";
		var drgCode3 = "";
		var drgCode4 = "";
		for (var i = 0; i < data.length; i++) {  
			var value1 = data[i][xyAx.xAxis.code];
			var value2 = data[i][xyAx.yAxis.code];
			if(value1!=null && value1!=undefined && value2!=null && value2!=undefined ){
				var v1 = parseFloat(value1)
				var v2 = parseFloat(value2);
				var drgCode = data[i].ddesc;
				if(v1>0 && v2>0){
					drgCode1 += drgCode+"、";
				}else if(v1<0 && v2>0){
					drgCode2 += drgCode+"、";
				}else if(v1<=0 && v2<=0){
					drgCode3 += drgCode+"、";
				}else if(v1>0 && v2<0){
					drgCode4 += drgCode+"、";
				}
			}
		}
	
	container.empty();
	var element = [];
	element.push("<table class='quadrant-table'>");
	element.push("<tr><td><font color = '#f05a72'><i class='icon-circle'></i></font>&nbsp;I象限:药占比和次均费用超标</td><td id='td1'></td></tr>");
	element.push("<tr><td><font color = '#5bc0de'><i class='icon-circle'></i></font>&nbsp;II象限:次均费用超标</td><td id='td2'></td></tr>");
	element.push("<tr><td><font color = '#66cc66'><i class='icon-circle'></i></font>&nbsp;III象限:药占比和次均费用未超标</td><td id='td3'></td></tr>");
	element.push("<tr><td><font color = '#fed503'><i class='icon-circle'></i></font>&nbsp;IV象限:药占比超标</td><td id='td4'></div></td></tr>");
	element.push("</table>");
	container.append(element.join("\n"));
	container.find('#td1').text(drgCode1.substr(0,drgCode1.length-1));
	container.find('#td2').text(drgCode2.substr(0,drgCode2.length-1));
	container.find('#td3').text(drgCode3.substr(0,drgCode3.length-1));
	container.find('#td4').text(drgCode4.substr(0,drgCode4.length-1));

}
	}
});
